package org.campuscontent.io;

import java.io.File;
import java.util.Hashtable;
import java.util.Vector;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.campuscontent.model.AutomataCellViewFactory;
import org.campuscontent.model.AutomataConstants;
import org.campuscontent.ui.AutomataGraph;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.GraphLayoutCache;
import org.jgraph.graph.GraphModel;

public class AutomataParser implements AutomataConstants
{
    private AutomataParser()
    {
    }
    
    public static AutomataParser getInstance() 
    {
        return self;
    }
    
    public AutomataGraph getGraphFrom( String filename )
    {
        try
        {
            LineIterator itLog = FileUtils.lineIterator( new File( filename ) );
            
            Vector<String> content = new Vector<String>( 10, 5 );
            while ( itLog.hasNext() )
            {
                String curr = itLog.nextLine().trim();
                if ( curr != null && !curr.equals( "" ) && !curr.startsWith( COMMENT ) )
                {
                    content.add( curr );
                }
            }
            
            Vector<Item> items = parseElements( content );
            
            if ( items == null ) return null;
            
            GraphModel model = new DefaultGraphModel();
    		GraphLayoutCache layoutCache = new GraphLayoutCache();
    		layoutCache.setFactory(new AutomataCellViewFactory());
    		AutomataGraph graph = new AutomataGraph(model, layoutCache);
            //result.initFrom( items );
            
            return graph;
        }
        catch ( Exception e )
        {
            e.printStackTrace();
        }
        
        return null;
    }
    
    protected Vector<Item> parseElements( Vector<String> source )
    {
        Vector<Item> result = new Vector<Item>( 10, 5 );
        
        int i = source.size();
        for ( int idx = 0; idx < i; idx++ )
        {
            String curr = source.elementAt( idx );
            Item it = new Item();
            
            if ( curr.equals( FileStream.NODE ) )  // node declaration found
            {
                it.type = FileStream.NODE;
            }
            else if ( curr.equals( FileStream.LINK ) )  // link declaration found
            {
                it.type = FileStream.LINK;
            }
                
            int j = idx + 2;
            while ( !curr.equals( END_DECLARATION ) )
            {
                curr = source.elementAt( j );
                
                String[] tokens = curr.split( "=" );
                if ( tokens.length == 2 )
                {
                    String key   = "";
                    String value = "";
                    
                    key   = tokens[ 0 ].trim();
                    value = tokens[ 1 ].trim();
                    value = value.substring( 1, value.length()-1 );
                    
                    if ( key.equals( "id" ) )
                    {
                        it.id = value;
                    }
                    
                    it.attributes.put( key, value );
                }
                
                j++;
            }
            
            idx = j-1;
            
            result.add( it );
        }
        
        if ( result.isEmpty() ) return null;
        
        return result;
    }
    
    public static void main( String[] args )
    {
        String filename = "C:\\workspace2\\graphpro\\config\\automata.def";
        
        AutomataParser ap = new AutomataParser();
        ap.getGraphFrom( filename );
    }
    
    public class Item
    {
        public String id  ;
        public String type;
        
        public String toString()
        {
            return "id: " + id + " type: " + type + " attrs: " + attributes;
        }
        
        public final Hashtable<String, String> attributes = new Hashtable<String, String>();
    }
    
    private static final AutomataParser self = new AutomataParser();
    
    private static final String COMMENT = "//";
    
    private static final String END_DECLARATION   = "}";
}
